﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Rabbit.Utils
{
    public class UrlEncoder
    {
        /// <summary>
        /// HttpUtility.UrlPathEncode() generates the lowercase characters, however we need uppercase
        /// chars when generating signatures in OAuth
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string UrlEncode(string value)
        {
            return Regex.Replace(HttpUtility.UrlEncode(value).Replace("+", "%20"),
                "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
                .Replace("%7E", "~")//~ will be encoded, however if sohu server will denied if we use the encoded result other than ~
                .Replace("(", "%28")//( won't be encoded, however we should encoded it to let the server accept our encoding
                .Replace(")", "%29")//) won't be encoded, however we should encoded it to let the server accept our encoding
                .Replace("*","%20");//replace * to be an empty string ,it will cause authorized failed
        }
    }
}
